#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Magpie - an open source discussion forum system.
#
# Copyright (C) 2009 The Magpie Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ajiaojr.commons.django.shortcuts import render_to_response

from ajiaojr.magpie import forms, models
from ajiaojr.magpie.views import build_template_params
from ajiaojr.magpie.views import forum_required

from django.http import Http404
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _

from google.appengine.api import memcache

def category(request):
  p = build_template_params(_('Category Configuration'), '/', request.user)
  p['categories'] = models.ForumCategory.get_full_forum_tree()
  return render_to_response(request, 'admin/category.html', p)

def category_add(request):
  if request.method == 'POST':
    f = forms.ForumCategoryForm(request.POST)
    if f.is_valid():
      f.save()
      memcache.delete('FORUM_TREE')
      return HttpResponseRedirect('/admin/category')
  else:
    f = forms.ForumCategoryForm()
  
  p = build_template_params(_('Add Category'), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/category/info.html', p)

def category_delete(request, category_id):
  category = models.ForumCategory.get_by_id(int(category_id))
  if category is not None:
    category.is_deleted = not category.is_deleted
    category.put()
    memcache.delete('FORUM_TREE')
  return HttpResponseRedirect('/admin/category')

def category_edit(request, category_id):
  category = models.ForumCategory.get_by_id(int(category_id))
  if category is not None:
    if request.method == 'POST':
      f = forms.ForumCategoryForm(request.POST, instance = category)
      if f.is_valid():
        f.save()
        memcache.delete('FORUM_TREE')
        return HttpResponseRedirect('/admin/category')
    else:
      f = forms.ForumCategoryForm(instance = category)
      
    p = build_template_params(_('Edit Category'), '/', request.user)
    p['form'] = f
    return render_to_response(request, 'admin/category/info.html', p)
  else:
    raise Http404

def clear_memcache(request):
  memcache.flush_all()
  return HttpResponseRedirect('/admin/')

def club(request):
  if request.method == 'POST':
    f = forms.ClubConfigForm(request.POST,
                             instance = models.ClubConfig.get_instance())
    if f.is_valid():
      f.save()
      memcache.delete('CLUB_CONFIG')
      return HttpResponseRedirect('/admin/')
  else:
    f = forms.ClubConfigForm(instance = models.ClubConfig.get_instance())

  p = build_template_params(_("Club Configuration"), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/club.html', p)

def forum(request):
  p = build_template_params(_('Forum Configuration'), '/', request.user)
  p['categories'] = models.ForumCategory.get_full_forum_tree()
  return render_to_response(request, 'admin/forum.html', p)

def forum_add(request):
  if request.method == 'POST':
    f = forms.ForumForm(request.POST)
    if f.is_valid():
      f.save()
      memcache.delete('FORUM_TREE')
      return HttpResponseRedirect('/admin/forum')
  else:
    f = forms.ForumForm()
    
  p = build_template_params(_('Add Forum'), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/forum/info.html', p)

def games(request):
  p = build_template_params(_('Games Configuration'), '/', request.user)
  return render_to_response(request, 'admin/games/index.html', p)

def games_mine(request):
  if request.method == 'POST':
    f = forms.MineGoldConfigForm(request.POST,
                             instance = models.MineGoldConfig.get_instance())
    if f.is_valid():
      f.save()
      memcache.delete('GAMES_MINE_GOLD_CONFIG')
      return HttpResponseRedirect('/admin/')
  else:
    f = forms.MineGoldConfigForm(instance = models.MineGoldConfig.get_instance())

  p = build_template_params(_("Mine Gold Configuration"), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/games/mine/index.html', p)

@forum_required
def forum_edit(request, forum_id):
  if request.method == 'POST':
    f = forms.ForumForm(request.POST, instance = request.forum)
    if f.is_valid():
      f.save()
      memcache.delete('FORUM_TREE')
      return HttpResponseRedirect('/admin/forum')
  else:
    f = forms.ForumForm(instance = request.forum)
    
  p = build_template_params(_('Edit Forum'), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/forum/info.html', p)

def group(request):
  groups = models.UserGroup.all()
  
  p = build_template_params(_('User Groups'), '/', request.user)
  p['groups'] = groups
  return render_to_response(request, 'admin/group.html', p)

def group_add(request):
  if request.method == 'POST':
    f = forms.UserGroupForm(request.POST)
    if f.is_valid():
      f.save()
      return HttpResponseRedirect('/admin/group')
    
  f = forms.UserGroupForm()
  p = build_template_params(_('Add Group'), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/group/info.html', p)

def group_edit(request, group_id):
  group = models.UserGroup.get_by_id(int(group_id))
  
  if group is None:
    raise Http404
  
  if request.method == 'POST':
    f = forms.UserGroupForm(request.POST, instance = group)
    if f.is_valid():
      f.save()
      return HttpResponseRedirect('/admin/group')
  else:
    f = forms.UserGroupForm(instance = group)
    
  p = build_template_params(_('Edit Group'), '/', request.user)
  p['form'] = f
  return render_to_response(request, 'admin/group/info.html', p)

def index(request):
  p = build_template_params(_('Admin'), '/', request.user)
  return render_to_response(request, 'admin/index.html', p)
